<template>
  <div>
    <el-container>
      <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
        <el-menu :default-openeds="['1']" router>
          <template v-for="item in routers">
            <el-submenu
              v-if="item.meta && !item.meta.hidden"
              :key="item.path"
              :index="item.path"
            >
              <template slot="title">
                <i :class="item.meta.icon"></i>
                {{ item.meta.title }}
              </template>
              <!-- 子级菜单 -->
              <template v-for="subItem in item.children">
                <el-menu-item v-if="subItem.meta && !subItem.meta.hidden" :key="subItem.path" :index="subItem.path">
                  <i :class="subItem.meta.icon"></i>
                  {{ subItem.meta.title }}
                </el-menu-item>
              </template>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>

      <el-container>
        <el-header style="text-align: right; font-size: 12px">
          <el-dropdown @command="handleCommand">
            <i class="el-icon-setting" style="margin-right: 15px"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="logout">登出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <span>{{ username }}</span>
        </el-header>

        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      routers: this.$router.options.routes
    };
  },
  computed: {
    username() {
      return this.$store.getters.username;
    }
  },
  methods: {
    handleCommand(command) {
      if (command == "logout") {
        this.logout();
      }
    },
    logout() {
      this.$store.dispatch("logout").then(() => {
        this.$router.push("/login");
      });
    }
  }
};
</script>

<style scoped>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>
